package com.mrx.compose.demo

import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import com.mrx.compose.demo.router.Router
import com.mrx.compose.demo.util.UserInfoUtil
import com.mrx.compose.demo.view.HomeView
import com.mrx.compose.demo.view.LoginView
import org.slf4j.LoggerFactory

object Main {

    private val logger = LoggerFactory.getLogger(this::class.java)

    @JvmStatic
    fun main(args: Array<String>) = application {
        Window(
            onCloseRequest = ::exitApplication, title = "HelloWorld",
            state = rememberWindowState(position = WindowPosition.Aligned(Alignment.Center)),
        ) { app() }
    }

    @Composable
    fun app() = MaterialTheme {
        val router = Router().addRouter {
            "/login".addRouter(LoginView)
            "/home".addRouter(HomeView)
        }
        var currentRouter by remember { mutableStateOf(if (UserInfoUtil.userToken == null) "/login" else "/home") }
        router.navigate(currentRouter) { view ->
            logger.info("showView: $view")
            view.showView { currentRouter = it }
        }
    }

}